﻿Public Class MainReservation
    Dim DAReservation As New DSReservationTableAdapters.V_RESERVATIONTableAdapter
    Dim DAReservationDetail As New DSReservationTableAdapters.RESERVATION_DETAILTableAdapter
    Dim DARoom As New DSFloorManagemantTableAdapters.ROOMSTableAdapter
    Dim DAFloor As New DSFloorManagemantTableAdapters.FLOORSTableAdapter
    Dim DACategory As New DSFloorManagemantTableAdapters.ROOM_CATEGORYTableAdapter
    Dim DAInvoiceReservation As New DSReservationTableAdapters.View_ReservationInvoiceTableAdapter
    Dim DARoomServationInfo As New DSReservationTableAdapters.RESERVATIONTableAdapter
    Public PrinterReceipt As String = My.Settings.PrinterName
    Public printDocument As New System.Drawing.Printing.PrintDocument

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        'With CboCategoryRoom
        '    .DataSource = DACategory.GetData
        '    .ValueMember = "CATEGORY_ID"
        '    .DisplayMember = "GATEGORY_NAME"
        '    .SelectedIndex = -1
        'End With

        'With CboFloor
        '    .DataSource = DAFloor.GetData
        '    .ValueMember = "FLOOR_ID"
        '    .DisplayMember = "FLOOR_NAME"
        '    .SelectedIndex = -1
        'End With
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private Sub BtnNewCheckIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNewCheckIn.Click
        If ListViewRoom.SelectedItems.Count = 0 Then
            'MenuCheckIn.Enabled = False
            'MenuCheckOut.Enabled = False
            'btnMoreService.Enabled = False
        Else

            Dim DataRooms As DataRow = ListViewRoom.Items(ListViewRoom.SelectedIndices(0)).Tag
            If DARoom.CheckRoomStatus(DataRooms("ROOM_ID")) > 0 Then
                MessageBox.Show("បន្ទប់ កំពុងភ្ញៀថសំរាក់ មិនអាចចុះឈ្មោះ បានទេ!", "Room", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
            Dim NewChecking As New FormNewCheckIn
            'NewChecking.Enabled = False
            NewChecking.cboRoom.SelectedValue = DataRooms("ROOM_ID")
            If NewChecking.ShowDialog() = DialogResult.OK Then
                If CDbl(EmptyString(NewChecking.TxtPrepay.Text)) > 0 Then
                    DARoom.UpdateRoomStatus(3, NewChecking.cboRoom.SelectedValue)
                End If
                RefreshRoomAvaliable()
            End If
        End If
    End Sub
    Private Sub RefreshReservation()
        GridReservation.DataSource = DAReservation.GetData
    End Sub
    Private Sub RefreshCheckInOnly()
        GridReservation.DataSource = DAReservation.GetReservationCheckIn
    End Sub
    Private Sub RefreshCheckOutOnly()
        GridReservation.DataSource = DAReservation.GetReservatonCheckOut
    End Sub
    Private Function CountAvailableRoom() As Integer
        Return DARoom.CountRoomAvailable
    End Function
    Private Function CountReservation() As Integer
        Return DAReservation.CountReservation
    End Function
    Private Function CountUnAvailable() As Integer
        Return DARoom.CountRoomUnavailable
    End Function
    Private Sub RefreshRoomAvaliable()
        Dim tblRoom As DataTable = DARoom.GetData
        ShowDataInLvw(tblRoom, ListViewRoom)
        lblTotalNotAvailable.Text = CountUnAvailable() & " បន្ទប់"
        lblRoomAvailable.Text = CountAvailableRoom() & " បន្ទប់"
        'lblStaying.Text = CountReservation() & " នាក់"
        ListViewRoom.Focus()
        ListViewRoom.Select()
    End Sub

    Private Sub BtnRefreshReservation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefreshReservation.Click
        RefreshAllTransaction()
    End Sub

    Private Sub RefreshAllTransaction()
        RefreshReservation()
        RefreshRoomAvaliable()
    End Sub

    Private Sub ShowDataInLvw(ByVal data As DataTable, ByVal lvw As ListView)
        ' lvw.View = View.Details
        'lvw.GridLines = True
        lvw.Columns.Clear()
        lvw.Items.Clear()
        Dim lst As ListViewItem
        'For Each col As DataColumn In data.Columns
        '    lvw.Columns.Add(col.ToString)
        'Next
        For Each row As DataRow In data.Rows

            lst = lvw.Items.Add(If(row("ROOM_NAME") IsNot Nothing, row("ROOM_NAME").ToString & " (គ្រែ " & row("ROOM_BED") & ")", ""))
            'Room not available
            If row("ROOM_STATUS") = 1 Then
                'lst.ForeColor = Color.Red
                'lst.Font = New Font("Khmer OS Battambang", 12, FontStyle.Bold, GraphicsUnit.Point)
                lst.ImageIndex = 1


                'Room have soft dring and not available
            ElseIf row("ROOM_STATUS") = 2 Then
                lst.ImageIndex = 2

                'Room have pre-paid
            ElseIf row("ROOM_STATUS") = 3 Then
                lst.ImageIndex = 3

                'Room have paid and not clean yet
            ElseIf row("ROOM_STATUS") = 4 Then
                lst.ImageIndex = 4
            Else
                'Room Available
                lst.ImageIndex = 0
            End If

            lst.Tag = row
            RefreshReservationDetail(0)
            'For i As Integer = 1 To data.Columns.Count - 1
            '    lst.SubItems.Add(If(row(i) IsNot Nothing, row(i).ToString, ""))
            'Next
        Next
    End Sub

   

    Private Sub ListViewRoom_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListViewRoom.ItemSelectionChanged
        If ListViewRoom.SelectedItems.Count = 0 Then
            ContextMenuMoreService.Enabled = False
            LblReservationID.Text = "0"
            LblRoomID.Text = "0"
            RefreshReservationDetail(LblReservationID.Text)
        Else
            ContextMenuMoreService.Enabled = True

            Dim row As DataRow = CType(e.Item.Tag, DataRow)
            If row("ROOM_STATUS") <> 0 Then
                MenuRoomInformation.Enabled = True
                MenuCheckIn.Enabled = False
                BtnNewCheckIn.Enabled = False
                BtnCheckOut.Enabled = True
                MenuCleanRoom.Enabled = False
                MenuCheckOut.Enabled = True
                BtnExtraService.Enabled = True
                btnMoreService.Enabled = True
                MenuDelete.Enabled = True
                If row("ROOM_STATUS") = 4 Then
                    MenuRoomInformation.Enabled = True
                    MenuCheckIn.Enabled = False
                    BtnNewCheckIn.Enabled = False
                    BtnCheckOut.Enabled = False
                    MenuCleanRoom.Enabled = True
                    BtnExtraService.Enabled = False
                    MenuCheckOut.Enabled = False
                    btnMoreService.Enabled = False
                    MenuDelete.Enabled = False
                End If
            Else
                MenuRoomInformation.Enabled = True
                MenuCheckIn.Enabled = True
                BtnNewCheckIn.Enabled = True
                BtnCheckOut.Enabled = False
                MenuCheckOut.Enabled = False
                btnMoreService.Enabled = False
                MenuDelete.Enabled = False
                MenuCleanRoom.Enabled = False
                BtnExtraService.Enabled = False
            End If


            Dim RoomReservTbl As DataTable = DARoomServationInfo.SelectRoomReservationInfo(row("ROOM_ID"))
            If RoomReservTbl.Rows.Count > 0 Then
                For Each drow As DataRow In RoomReservTbl.Rows
                    LblReservationID.Text = drow("RESERVATION_ID")
                    LblRoomID.Text = drow("ROOM_ID")
                Next
            Else
                LblReservationID.Text = "0"
                LblRoomID.Text = "0"
            End If
            RefreshReservationDetail(LblReservationID.Text)
            End If
        'Try
        '    Dim row As DataRow = CType(e.Item.Tag, DataRow)
        '    'LblSaveOption.Text = row("ROOM_ID")
        '    'TxtRoomName.Text = row("ROOM_NAME")
        '    'CboCategoryRoom.SelectedValue = row("CATEGORY_ID")
        '    'CboFloor.SelectedValue = row("FLOOR_ID")
        '    'TxtBed.Text = row("ROOM_BED")
        '    'TxtDescriptRoom.Text = row("ROOM_DESCRPTION")
        'Catch ex As Exception

        'End Try
        
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExtraService.Click
        If ListViewRoom.SelectedItems.Count = 0 Then Exit Sub
        If LblReservationID.Text = "0" Then Exit Sub
        Dim Service As New ForMoreExtraservice(Me)
        Service.lblReservationID.Text = LblReservationID.Text
        If Service.ShowDialog = DialogResult.OK Then
            DARoom.UpdateRoomStatus(2, LblRoomID.Text)
            RefreshRoomAvaliable()
        End If
    End Sub

    Private Sub btnMoreService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoreService.Click
        If ListViewRoom.SelectedItems.Count = 0 Then Exit Sub
        If LblReservationID.Text = "0" Then Exit Sub
        Dim Service As New ForMoreExtraservice(Me)
        Service.lblReservationID.Text = LblReservationID.Text
        If Service.ShowDialog = DialogResult.OK Then
            DARoom.UpdateRoomStatus(2, LblRoomID.Text)
            RefreshRoomAvaliable()
        End If
    End Sub
    Public Sub RefreshReservationDetail(ByVal ReservationID As Integer)
        GridReservationDedail.DataSource = DAReservationDetail.SelectReservationByID(ReservationID)
    End Sub

    Private Sub MainReservation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GroupBox1.Visible = False
        ListViewRoom.Visible = True
        ListViewRoom.Dock = DockStyle.Fill
        RefreshRoomAvaliable()
    End Sub

    Private Sub GridReservation_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridReservation.SelectionChanged
        Try
            If GridReservation.GetRow.Cells("CHECK_IN_OUT_STATUS").Value = 2 Then
                BtnPrintInvoice.Enabled = True
                'ToolStripButton1.Enabled = False
                'btnMoreService.Enabled = False
                'BtnRemoveService.Enabled = False
            Else
                BtnPrintInvoice.Enabled = False
                'ToolStripButton1.Enabled = True
                'btnMoreService.Enabled = True
                'BtnRemoveService.Enabled = True
            End If
            RefreshReservationDetail(GridReservation.GetRow.Cells("RESERVATION_ID").Value)
        Catch ex As Exception
            RefreshReservationDetail(0)
        End Try
    End Sub

    Private Sub BtnRemoveService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRemoveService.Click
        If GridReservationDedail.SelectedItems.Count = 0 Then Exit Sub
        If ListViewRoom.SelectedItems.Count = 0 Then Exit Sub
        If LblReservationID.Text = "0" Then Exit Sub
        If MessageBox.Show("Do you want to remove service?", "Service", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            If DAReservationDetail.DeleteReservationDetail(GridReservationDedail.GetRow.Cells("RESERVATION_DE_ID").Value) = 1 Then
                RefreshReservationDetail(LblReservationID.Text)
                If GridReservationDedail.SelectedItems.Count = 0 Then
                    DARoom.UpdateRoomStatus(1, LblRoomID.Text)
                    RefreshRoomAvaliable()
                End If

            Else
                MessageBox.Show("Error remove service", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCheckOut.Click
        'Try
        If LblReservationID.Text = "0" Then Exit Sub
        If LblRoomID.Text = "0" Then Exit Sub
        'If GridReservation.GetRow.Cells("CHECK_IN_OUT_STATUS").Value = 2 Then Exit Sub
        Dim CheckOut As New FormCheckOut

        Dim RoomReservTbl As DataTable = DAReservation.SelectRoomReservation(LblRoomID.Text)

        For Each drow As DataRow In RoomReservTbl.Rows
            'LblReservationID.Text = drow("RESERVATION_ID")
            'LblRoomID.Text = drow("ROOM_ID")
            CheckOut.LblReservation.Text = drow("RESERVATION_ID")
            CheckOut.lblRoomID.Text = drow("ROOM_ID")
            CheckOut.TxtGuestName.Text = drow("CUSTOMER_NAME")
            CheckOut.TxtRoomNo.Text = drow("ROOM_NAME")
            CheckOut.TxtBed.Text = drow("ROOM_BED")
            CheckOut.TxtDateIn.Text = Format(drow("CHECK_IN_DATE"), "dd/MM/yyyy")
            CheckOut.TxtPrePaid.Text = drow("PREPAY")
            CheckOut.TimeIn.Value = drow("TIME_IN")

        Next


        If CheckOut.ShowDialog = DialogResult.OK Then
            'RefreshCheckInOnly()
            RefreshRoomAvaliable()
        End If
        'Catch ex As Exception

        'End Try

    End Sub

    Private Sub BtnShowOnlyCheckIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShowOnlyCheckIn.Click
        'RefreshCheckInOnly()
        BtnPrintInvoice.Enabled = False
        GroupBox1.Visible = False
        ListViewRoom.Visible = True
        ListViewRoom.Dock = DockStyle.Fill
        RefreshRoomAvaliable()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        RefreshCheckOutOnly()
        RefreshRoomAvaliable()
    End Sub

    Private Sub GroupBox4_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub BtnFindInvoice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFindInvoice.Click
        Dim FFindCheckOut As New FormFindCheckOut(Me)
        If FFindCheckOut.ShowDialog() = DialogResult.OK Then
            GroupBox1.Visible = True
            ListViewRoom.Visible = False
            GroupBox1.Dock = DockStyle.Fill
        End If


    End Sub
    Public Sub FindGuestCheckOut(ByVal dateFrom As Date, ByVal dateTo As Date)
        GridReservation.DataSource = DAReservation.FindGuestCheckOut(dateFrom.Date, dateTo.Date)
    End Sub

    Private Sub BtnPrintInvoice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrintInvoice.Click
        Try
            Dim Invoice As New Invice
            Dim tblReservation As DataTable = DAInvoiceReservation.GetInvoiceByReservationID(GridReservation.GetRow.Cells("RESERVATION_ID").Value)
            Invoice.SetDataSource(tblReservation)
            printDocument.PrinterSettings.PrinterName = PrinterReceipt.Trim
            Invoice.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName
            Invoice.PrintToPrinter(1, False, 1, 1)
            Invoice.Dispose()
            Invoice.Close()

        Catch ex As Exception
            MessageBox.Show("សូមមេតា ពិននិត្យ Printer", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub MenuCheckIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCheckIn.Click
        'Try

        If ListViewRoom.SelectedItems.Count = 0 Then
            'MenuCheckIn.Enabled = False
            'MenuCheckOut.Enabled = False
            'btnMoreService.Enabled = False
        Else

            Dim DataRooms As DataRow = ListViewRoom.Items(ListViewRoom.SelectedIndices(0)).Tag
            If DARoom.CheckRoomStatus(DataRooms("ROOM_ID")) > 0 Then
                MessageBox.Show("បន្ទប់ កំពុងភ្ញៀថសំរាក់ មិនអាចចុះឈ្មោះ បានទេ!", "Room", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
            Dim NewChecking As New FormNewCheckIn
            'NewChecking.Enabled = False
            NewChecking.cboRoom.SelectedValue = DataRooms("ROOM_ID")
            If NewChecking.ShowDialog() = DialogResult.OK Then
                If CDbl(EmptyString(NewChecking.TxtPrepay.Text)) > 0 Then
                    DARoom.UpdateRoomStatus(3, NewChecking.cboRoom.SelectedValue)
                End If
                RefreshRoomAvaliable()
            End If
        End If

        'Catch ex As Exception

        'End Try

    End Sub

    Private Sub MenuRoomInformation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuRoomInformation.Click
        If ListViewRoom.SelectedItems.Count = 0 Then
            'MenuCheckIn.Enabled = False
            'MenuCheckOut.Enabled = False
            'btnMoreService.Enabled = False
        Else

            Dim DataRooms As DataRow = ListViewRoom.Items(ListViewRoom.SelectedIndices(0)).Tag
            Dim FRoomInfo As New FormRoomInfo
            'NewChecking.Enabled = False
            FRoomInfo.LblSaveOption.Text = DataRooms("ROOM_ID")
            FRoomInfo.TxtRoomName.Text = DataRooms("ROOM_NAME")
            FRoomInfo.CboCategoryRoom.SelectedValue = DataRooms("CATEGORY_ID")
            FRoomInfo.CboFloor.SelectedValue = DataRooms("FLOOR_ID")
            FRoomInfo.TxtBed.Text = DataRooms("ROOM_BED")
            FRoomInfo.TxtDescriptRoom.Text = DataRooms("ROOM_DESCRPTION")
            FRoomInfo.ShowDialog()
            'If FRoomInfo.ShowDialog() = DialogResult.OK Then
            '    'RefreshCheckInOnly()
            '    RefreshRoomAvaliable()
            'End If
        End If





    End Sub

    Private Sub MenuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDelete.Click
        If ListViewRoom.SelectedItems.Count = 0 Then Exit Sub
        If LblReservationID.Text = "0" Then Exit Sub
        If LblRoomID.Text = "0" Then Exit Sub
        If MessageBox.Show("តើ នាក់ចង់លុបកាស្នាក់នេះឬ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            If DARoom.UpdateRoomStatus(0, LblRoomID.Text) = 1 And DARoomServationInfo.IsDeleteOrUndo(2, LblReservationID.Text) Then
                RefreshRoomAvaliable()
                LblReservationID.Text = 0
                LblRoomID.Text = 0
            End If

        End If
    End Sub

    Private Sub MenuCleanRoom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCleanRoom.Click
        If ListViewRoom.SelectedItems.Count = 0 Then
            Exit Sub
        Else
            Dim row As DataRow = CType(ListViewRoom.Items(ListViewRoom.SelectedIndices(0)).Tag, DataRow)
            DARoom.UpdateRoomStatus(0, row("ROOM_ID"))
            RefreshRoomAvaliable()
        End If
    End Sub

    Private Sub MenuCheckOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCheckOut.Click
        'Try
        If LblReservationID.Text = "0" Then Exit Sub
        If LblRoomID.Text = "0" Then Exit Sub
        'If GridReservation.GetRow.Cells("CHECK_IN_OUT_STATUS").Value = 2 Then Exit Sub
        Dim CheckOut As New FormCheckOut

        Dim RoomReservTbl As DataTable = DAReservation.SelectRoomReservation(LblRoomID.Text)

        For Each drow As DataRow In RoomReservTbl.Rows
            'LblReservationID.Text = drow("RESERVATION_ID")
            'LblRoomID.Text = drow("ROOM_ID")
            CheckOut.LblReservation.Text = drow("RESERVATION_ID")
            CheckOut.lblRoomID.Text = drow("ROOM_ID")
            CheckOut.TxtGuestName.Text = drow("CUSTOMER_NAME")
            CheckOut.TxtRoomNo.Text = drow("ROOM_NAME")
            CheckOut.TxtBed.Text = drow("ROOM_BED")
            CheckOut.TxtDateIn.Text = Format(drow("CHECK_IN_DATE"), "dd/MM/yyyy")
            CheckOut.TxtPrePaid.Text = drow("PREPAY")
            CheckOut.TimeIn.Value = drow("TIME_IN")

        Next


        If CheckOut.ShowDialog = DialogResult.OK Then
            'RefreshCheckInOnly()
            RefreshRoomAvaliable()
        End If
        'Catch ex As Exception

        'End Try
    End Sub

    Private Sub BtnChangeRoom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnChangeRoom.Click

        If ListViewRoom.SelectedItems.Count = 0 Then Exit Sub
        If LblReservationID.Text = "0" Then Exit Sub
        If LblRoomID.Text = "0" Then Exit Sub
        Dim ChangeRoom As New FormChangeRoom
        Dim DataRooms As DataRow = ListViewRoom.Items(ListViewRoom.SelectedIndices(0)).Tag
        ChangeRoom.lblRoomStatus.Text = DataRooms("ROOM_STATUS")
        ChangeRoom.cboRoom.SelectedValue = LblRoomID.Text
        ChangeRoom.cboRoom.Enabled = False
        ChangeRoom.LblReservationID.Text = LblReservationID.Text
        If ChangeRoom.ShowDialog() = DialogResult.OK Then
            RefreshRoomAvaliable()
        End If
    End Sub
End Class
